<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Model\Logic;

use App\Model\Entity\Member;
use App\Model\Entity\MemberDetail;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Db\DB;


/**
 * Class UserService
 *
 * @since 2.0
 *
 * @Bean()
 */
class MemberLogic
{
    public function verifyUser(array $param): array
    {
        $member = Member::where($param)->first()->toArray();

        if($member){
            return $member;
        }
        return [];
    }

    public function registerUser(array $attributes): int
    {
        $attributes['add_time'] = time();
        $attributes['last_update_time'] = time();

        $member = Member::new($attributes);
        $member->save();
        $member_id = $member->getId();

        if($member_id){
            $arr = [
                'member_id'=>$member_id,
                'add_time'=>time(),
                'last_update_time'=>time()
            ];
            MemberDetail::insertGetId($arr);
            return $member_id;
        }else{
            return 0;
        }

    }

    public function getUser(int $user_id): array
    {
        $member = Member::findOrFail((string)$user_id)->toArray();
        if($member){
            return $member;
        }
        return [];
    }

    public function getUserDetail(string $user_id):array{
        $member = DB::table('member')
            ->join('member_detail', 'member.id', '=', 'member_detail.member_id')
            ->join('locations', 'member.address_id', '=', 'locations.id')
            ->select("*")
            ->where('member.id',$user_id)
            ->get()->toArray();
        if($member){
            return $member;
        }
        return [];
    }
}
